package com.aldobucchi.x.flash.events
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import com.aldobucchi.utils.AbstractTest;

	public class EventListenerProxyTest extends AbstractTest
	{
		

		
		private var events:Array = [];
		
		private var preventDefault:Boolean = false;
		
		
		public function testMisc( ):void
		{
			
			// setup everything
			
			var d1	:IEventDispatcher 	= new EventDispatcher( );
			var els	:EventListenerSpec 	= new EventListenerSpec( Event.CHANGE, d1_change );

			var proxy:EventListenerProxy = els.addToDispatcherUsingProxy( d1 );
			
			proxy.addEventListener( EventHookEvent.FIRE, proxy_fire );


			// dispatch an event

			d1.dispatchEvent( new Event( Event.CHANGE ) );
		
		
			// assertions
		
			assertEquals( events.length , 2 );
			
			assertEquals(  events[0].type, EventHookEvent.FIRE );
			assertEquals(  events[1].type, Event.CHANGE );
		
		
			// dispatch another event, but this time we will preventDefault
			
			preventDefault = true;

			d1.dispatchEvent( new Event( Event.CHANGE ) );
		

			// assertions
		
			assertEquals( events.length , 3 ); // only one event was added because we prevented default
			
			assertEquals(  events[0].type, EventHookEvent.FIRE );
			assertEquals(  events[1].type, Event.CHANGE );		
			assertEquals(  events[2].type, EventHookEvent.FIRE );		
		
		
		
			// remove proxy ( which will remove the event listener as well
			preventDefault = false; // controlled variable
			proxy.remove( );
		
			d1.dispatchEvent( new Event( Event.CHANGE ) );
			
			
			
			// assertions
		
			assertEquals( events.length , 3 ); // no changes, we removed the proxy	
			
		
		}
		


		
		private function proxy_fire( event:EventHookEvent ):void
		{
			if ( preventDefault )
				event.preventDefault( );
			
			events.push( event );		
		}
		
		
		private function d1_change( event:Event ):void
		{
			events.push( event );			
		}

		
		
		
	}
}